package deployr.cli;

import static java.text.MessageFormat.*;

import java.net.*;

import deployr.domain.*;

public class Json {

	private final StringBuilder builder = new StringBuilder();

	public Json(ObjectMap map) {
		for (String key : map.keySet()) {
			append(key, map.get(key));
		}
	}

	private void append(String key, Object object) {
		builder.append(format("\"{0}\":\"{1}\",", key, URLEncoder.encode(S(object))));
	}

	private String S(Object object) {
		if(null == object) return "";
		return object.toString();
	}

	@Override
	public String toString() {
		return "{" + format("{0}", builder.toString().replaceAll(",$", "")) + "}";
	}

}
